# 
# Steven W. Webster
# "Anger and Declining Trust in Government in the American Electorate"
# Replication code for ANES analysis
#

setwd("")

library(dplyr)
library(ggplot2)
library(car)
library(stargazer)
library(MASS)

# Load data
anes <- read.delim("anes_timeseries_2012_rawdata.txt", sep = "|")

# Party ID
anes$pid_x[anes$pid_x == -2] <- NA
na.omit(anes$pid_x)

anes <- mutate(anes, democrat = pid_x <= 3 & pid_x > -2)
anes <- mutate(anes, republican = pid_x >= 5)
anes <- mutate(anes, independent = pid_x == 4)
anes <- mutate(anes, strong_partisan = as.numeric((pid_x == 1 | pid_x == 7)))

anes <- anes %>%
  mutate(dem = as.numeric(democrat == TRUE),
         rep = as.numeric(republican == TRUE),
         indy = as.numeric(independent == TRUE))

# Ideology
anes <- rename(anes, ideology = libcpo_self)
anes$ideology[anes$ideology < 1] <- NA
na.omit(anes$ideology)

anes$ideology[anes$democrat == TRUE & !is.na(anes$democrat)] <- recode(anes$ideology[anes$democrat == TRUE & !is.na(anes$democrat)], "1=7;2=6;3=5;4=4;5=3;6=2;7=1")

# Female dummy
anes$female <- as.numeric(anes$gender_respondent == 2)

# Non-white dummy
anes$dem_raceeth[anes$dem_raceeth < 1] <- NA
na.omit(anes$dem_raceeth)
anes$nonwhite <- as.numeric(anes$dem_raceeth != 1)

# Age
anes$dem_age_r_x[anes$dem_age_r_x == -2] <- NA
na.omit(anes$dem_age_r_x)
anes <- rename(anes, age = dem_age_r_x)

# Education
anes$dem_edu[anes$dem_edu == -9] <- NA
na.omit(anes$dem_edu)
anes$education <- NULL
anes$education[anes$dem_edu <= 9] <- 1  # code those with HS and less as 1
anes$education[anes$dem_edu > 9 & anes$dem_edu <= 13] <- 2 # some college up to a BA
anes$education[anes$dem_edu > 13] <- 3 # grad/professional school 

# Activism scale
anes <- rename(anes, attend_rally = mobilpo_rally) # does respondent attend rallies/speeches/etc
anes$attend_rally[anes$attend_rally < 0] <- NA
na.omit(anes$attend_rally)
anes$attend_rally <- recode(anes$attend_rally, "1=1;2=0")

anes <- rename(anes, talk_to_others = mobilpo_rmob) # does respondent try to persuade others/does respondent talk about politics to others
anes$talk_to_others[anes$talk_to_others < 0] <- NA
na.omit(anes$talk_to_others)
anes$talk_to_others <- recode(anes$talk_to_others, "1=1;2=0")

anes <- rename(anes, display_sign = mobilpo_sign) # does respondent wear button/display bumper sticker or yard signs
anes$display_sign[anes$display_sign < 0] <- NA
na.omit(anes$display_sign)
anes$display_sign <- recode(anes$display_sign, "1=1;2=0")

anes <- rename(anes, work_for_party = mobilpo_otherwork) # does respondent work or party or candianese
anes$work_for_party[anes$work_for_party < 0] <- NA
na.omit(anes$work_for_party)
anes$work_for_party <- recode(anes$work_for_party, "1=1;2=0")

anes <- rename(anes, donate_money = mobilpo_ctbcand) # does respondent donate to a candianese
anes$donate_money[anes$donate_money < 0] <- NA
na.omit(anes$donate_money)
anes$donate_money <- recode(anes$donate_money, "1=1;2=0")

anes <- rename(anes, donate_to_party = mobilpo_ctbpty) # does respondent donate to a party
anes$donate_to_party[anes$donate_to_party < 0] <- NA
na.omit(anes$donate_to_party)
anes$donate_to_party <- recode(anes$donate_to_party, "1=1;2=0")

anes <- rename(anes, donate_to_other = mobilpo_ctboth) # does respondent donate to a third-party group
anes$donate_to_other[anes$donate_to_other < 0] <- NA
na.omit(anes$donate_to_other)
anes$donate_to_other <- recode(anes$donate_to_other, "1=1;2=0")

anes <- rename(anes, attend_march = dhsinvolv_march) # did respondent attend a march
anes$attend_march[anes$attend_march < 0] <- NA
na.omit(anes$attend_march)
anes$attend_march <- recode(anes$attend_march, "1=1;2=0")

anes <- rename(anes, attend_school_board = dhsinvolv_board) # did respondent attend a school board meeting
anes$attend_school_board[anes$attend_school_board < 0] <- NA
na.omit(anes$attend_school_board)
anes$attend_school_board <- recode(anes$attend_school_board, "1=1;2=0")

anes <- rename(anes, sign_petition = dhsinvolv_petition) # did respondent sign a petition
anes$sign_petition[anes$sign_petition < 0] <- NA
na.omit(anes$sign_petition)
anes$sign_petition <- recode(anes$sign_petition, "1=1;2=0")

anes <- rename(anes, contact_mc = dhsinvolv_contact1) # did respondent contact a member of congress
anes$contact_mc[anes$contact_mc < 0] <- NA
na.omit(anes$contact_mc)
anes$contact_mc <- recode(anes$contact_mc, "1=1;2=0")

anes <- mutate(anes, activism = attend_rally + talk_to_others + display_sign + work_for_party +
                 donate_money + donate_to_party + donate_to_other + attend_march +
                 attend_school_board + sign_petition + contact_mc)

# Create affective anger scores for opposing party's candidate
# Remove missing values
anes$candaff_angrpcoft[anes$candaff_angrpcoft < 1] <- NA
anes$candaff_angdpcoft[anes$candaff_angdpcoft < 1] <- NA
na.omit(anes$candaff_angrpcoft)
na.omit(anes$candaff_angdpcoft)

# Recode variables so that higher values indicate more frequent feelings of anger
anes$candaff_angrpcoft <- recode(anes$candaff_angrpcoft, "5=1;4=2;3=3;2=4;1=5")
anes$candaff_angdpcoft <- recode(anes$candaff_angdpcoft, "5=1;4=2;3=3;2=4;1=5")

# Create anger scores for opposing party's candidate
anes$anger_oppos <- ifelse(anes$democrat == TRUE, anes$candaff_angrpcoft, ifelse(anes$republican == TRUE, anes$candaff_angdpcoft, NA))

# Anger toward either candidate as an independent variable
anes$anger_total <- anes$candaff_angrpcoft + anes$candaff_angdpcoft

# Clean variables for trust in government
anes <- anes %>%
  rename(govt_crooked = trustgvpo_crook, govt_cares = efficpo_carestd,
         no_say_govt = efficpo_saystd)

anes$govt_crooked[anes$govt_crooked < 1] <- NA
na.omit(anes$govt_crooked)

# Recode so that higher values indicate more crooked people in govt
anes$govt_crooked <- recode(anes$govt_crooked, "1=3;2=2;3=1")

anes$govt_cares[anes$govt_cares < 1] <- NA
na.omit(anes$govt_cares)

# Recode so that higher values indicate more agreement
# with statement that "public officials don't care what people think"
anes$govt_cares <- recode(anes$govt_cares, "1=5;2=4;3=3;4=2;5=1")

anes$no_say_govt[anes$no_say_govt < 1] <- NA
na.omit(anes$no_say_govt)

# Recode so that hgiher values indicate more agreement with
# statement that "people have no say about what govt does"
anes$no_say_govt <- recode(anes$no_say_govt, "1=5;2=4;3=3;4=2;5=1")

# Clean pre-election measure of trust in govt
anes$trustgov_trustgstd[anes$trustgov_trustgstd < 1] <- NA
na.omit(anes$trustgov_trustgstd)
anes$trustgov_trustgstd2 <- recode(anes$trustgov_trustgstd, "1=4;2=3;3=2;4=1")
anes <- anes %>% rename(trust_gov_pre = trustgov_trustgstd2)

# Aadditional trust variables
anes$trustgov_trustgrev[anes$trustgov_trustgrev < 1] <- NA
na.omit(anes$trustgov_trustgrev)

anes$trustgov_waste[anes$trustgov_waste < 1] <- NA
na.omit(anes$trustgov_waste)
anes$trustgov_waste <- recode(anes$trustgov_waste, "1=3;2=2;3=1")

anes$trustgov_bigintrst[anes$trustgov_bigintrst < 1] <- NA
na.omit(anes$trustgov_bigintrst)
anes <- anes %>%
  mutate(big_interest = as.numeric(trustgov_bigintrst == 1))


# Summary statistics
sstats <- anes %>%
  select(democrat, republican, independent, female, nonwhite, education)

stargazer(sstats,
          type="latex",
          style="apsr",
          title="Summary Statistics of 2012 ANES Data",
          covariate.labels=c("Democrat","Republican","Independent","Female","Non-white","Education"),
          out="",
          label="tab:anes_sstats")

# Rescale variables to range from 0-1
anes$anger_oppos_01 <- (anes$anger_oppos - min(anes$anger_oppos, na.rm=T)) / (max(anes$anger_oppos, na.rm=T) - min(anes$anger_oppos, na.rm=T))
anes$anger_total_01 <- (anes$anger_total - min(anes$anger_total, na.rm=T)) / (max(anes$anger_total, na.rm=T) - min(anes$anger_total, na.rm=T))
anes$ideology_01 <- (anes$ideology - min(anes$ideology, na.rm=T)) / (max(anes$ideology, na.rm=T) - min(anes$ideology, na.rm=T))
anes$education_01 <- (anes$education - min(anes$education, na.rm=T)) / (max(anes$education, na.rm=T) - min(anes$education, na.rm=T))
anes$activism_01 <- (anes$activism - min(anes$activism, na.rm=T)) / (max(anes$activism, na.rm=T) - min(anes$activism, na.rm=T))
anes$trust_gov_pre_01 <- (anes$trust_gov_pre - min(anes$trust_gov_pre, na.rm=T)) / (max(anes$trust_gov_pre, na.rm=T) - min(anes$trust_gov_pre, na.rm=T))


# Models for Table 1
# Targeted political anger
m1 <- lm(govt_crooked ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = anes)
m2 <- lm(govt_cares ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = anes)
m3 <- lm(no_say_govt ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = anes)

# Models for Table 2
# Models of targeted political anger and
# accounting for strength of partisanship
m4 <- lm(govt_crooked ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)
m5 <- lm(govt_cares ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)
m6 <- lm(no_say_govt ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)

# Models of generalized apolitical anger
m7 <- lm(govt_crooked ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)
m8 <- lm(govt_cares ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)
m9 <- lm(no_say_govt ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01 + strong_partisan, data = anes)

###

# Supplemental analyses for appendix
# Models from Table 1 (ordered logits)
m1b <- polr(as.factor(govt_crooked) ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, method = "logistic", data = anes)
m2b <- polr(as.factor(govt_cares) ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, method = "logistic", data = anes)
m3b <- polr(as.factor(no_say_govt) ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, method = "logistic", data = anes)

# Alternative dependent variables
alt_m1 <- lm(trustgov_trustgrev ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01, data = anes)
alt_m2 <- lm(trustgov_waste ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01, data = anes)
alt_m3 <- glm(big_interest ~ anger_oppos_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01, data = anes, family = "binomial")

# Standardized models
anes$anger_oppos_std <- (anes$anger_oppos - mean(anes$anger_oppos, na.rm=T))/sd(anes$anger_oppos, na.rm=T)
anes$democrat_std <- (anes$democrat - mean(anes$democrat, na.rm=T))/sd(anes$democrat, na.rm=T)
anes$ideology_std <- (anes$ideology - mean(anes$ideology, na.rm=T))/sd(anes$ideology, na.rm=T)
anes$female_std <- (anes$female - mean(anes$female, na.rm=T))/sd(anes$female, na.rm=T)
anes$nonwhite_std <- (anes$nonwhite - mean(anes$nonwhite, na.rm=T))/sd(anes$nonwhite, na.rm=T)
anes$education_std <- (anes$education - mean(anes$education, na.rm=T))/sd(anes$education, na.rm=T)
anes$activism_std <- (anes$activism - mean(anes$activism, na.rm=T))/sd(anes$activism, na.rm=T)
anes$trust_gov_pre_std <- (anes$trust_gov_pre - mean(anes$trust_gov_pre, na.rm=T))/sd(anes$trust_gov_pre, na.rm=T)
anes$strong_partisan_std <- (anes$strong_partisan - mean(anes$strong_partisan, na.rm=T))/sd(anes$strong_partisan, na.rm=T)

m1_std <- lm(govt_crooked ~ anger_oppos_std + democrat_std + ideology_std + female_std + nonwhite_std + education_std + activism_std + trust_gov_pre_std + strong_partisan_std, data = anes)
m2_std <- lm(govt_cares ~ anger_oppos_std + democrat_std + ideology_std + female_std + nonwhite_std + education_std + activism_std + trust_gov_pre_std + strong_partisan_std, data = anes)
m3_std <- lm(no_say_govt ~ anger_oppos_std + democrat_std + ideology_std + female_std + nonwhite_std + education_std + activism_std + trust_gov_pre_std + strong_partisan_std, data = anes)

# Regression models subsetted by co-partisans (e.g. Democrats)
m1_dems <- lm(govt_crooked ~ anger_oppos_01 + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = subset(anes, democrat == TRUE))
m2_dems <- lm(govt_cares ~ anger_oppos_01 + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = subset(anes, democrat == TRUE))
m3_dems <- lm(no_say_govt ~ anger_oppos_01 + ideology_01 + female + nonwhite + education_01 + activism_01 + trust_gov_pre_01, data = subset(anes, democrat == TRUE))

# Different dependent variables with generalized apolitical anger
ga_diff_dv1 <- lm(trustgov_trustgrev ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + strong_partisan, data = anes)
ga_diff_dv2 <- lm(trustgov_waste ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + strong_partisan, data = anes)
ga_diff_dv3 <- glm(big_interest ~ anger_total_01 + democrat + ideology_01 + female + nonwhite + education_01 + activism_01 + strong_partisan, data = anes, family = "binomial")

